//
//  NSMutableArray+CrashIntercept.m
//  Pods
//
//  Created by 余思豪 on 2022/9/22.
//

#import <Foundation/Foundation.h>
#import <objc/message.h>

@implementation NSMutableArray (CrashIntercept)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = NSClassFromString(@"__NSArrayM");
        // 增
        Method method_1 = class_getInstanceMethod(class, @selector(insertObject:atIndex:));
        Method method_2 = class_getInstanceMethod(class, @selector(YSH_insertObject:atIndex:));
        method_exchangeImplementations(method_1, method_2);
        // 删
        Method method_3 = class_getInstanceMethod(class, @selector(removeObjectAtIndex:));
        Method method_4 = class_getInstanceMethod(class, @selector(YSH_removeObjectAtIndex:));
        method_exchangeImplementations(method_3, method_4);
        // 改
        Method method_5 = class_getInstanceMethod(class, @selector(replaceObjectAtIndex:withObject:));
        Method method_6 = class_getInstanceMethod(class, @selector(YSH_replaceObjectAtIndex:withObject:));
        method_exchangeImplementations(method_5, method_6);
    });
}

// 增
- (void)YSH_insertObject:(id)anObject atIndex:(NSUInteger)index {
    if (!anObject) return;
    if (index > self.count) return;
    [self YSH_insertObject:anObject atIndex:index];
}

// 删
- (void)YSH_removeObjectAtIndex:(NSUInteger)index {
    if (index >= self.count) return;
    [self YSH_removeObjectAtIndex:index];
}

// 改
- (void)YSH_replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
    if (!anObject) return;
    if (index >= self.count) return;
    [self YSH_replaceObjectAtIndex:index withObject:anObject];
}

@end
